﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
//using MotivLoggerSettings;
namespace MotivLogger

{
    public class Logger : LoggerBase
    {
        private Settings LoggerSettings { get; set; }
        private Object lockObj;
        private List<ILogMethod> LogMethods;
        private bool paused = false;
  
        public Logger()
        {
            this.LoggerSettings = Settings.getSetting("logger");
            this.lockObj=new Object();
            this.LogMethods = new List<ILogMethod>(5);
            this.LoadLogMethods();
            
        }

        private void LoadLogMethods() 
        {
            try
            {
                Type LogMethodInterface = typeof(ILogMethod);
                var types = Assembly.GetAssembly(typeof(Logger)).GetTypes();
                var logmethodstypes = types.Where(LogMethodInterface.IsAssignableFrom).Where(t => t != LogMethodInterface).Where(t => this.LoggerSettings.child["logmethods"].child[t.Name.ToLower()].GetData("status") == "on");
                foreach (var logmethod in logmethodstypes)
                {
                    ILogMethod method = (ILogMethod)Activator.CreateInstance(logmethod);
                    this.LogMethods.Add(method);
                }
            }
            catch { }
        }

        public override void Log(MotivLogger.LogLevels Level, string Message, string Actor) 
        {
            if (!paused)
            {
                lock (lockObj)
                {

                    foreach (var LogMethod in this.LogMethods)
                        LogMethod.Log(Level, Message, Actor);
                }
            }
        }

        public override void Pause()
        {
            paused = true;
        }

        public override void Resume()
        {
            paused = false;
        }

        public override void Stop() 
        {
            foreach (var LogMethod in this.LogMethods)
                LogMethod.Stop();
        }

    }
}



